package edu.cuit.avatar;

import java.util.StringJoiner;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/9 14:35
 */
public class StringJoinerTest {
    public static void main(String[] args) {
        String[] names = {"Peppa", "Emily", "Pedro", "Dannie"};
        String s = toString(names);
        System.out.println(s);

        System.out.println(toString2(names));
        //使用String上的静态方法连接字符串
        String join = String.join(", ", names);
        System.out.println(join);
    }

    public static String toString(String[] list){
        StringBuilder sb = new StringBuilder("");
        sb.append('[');
        for(String e : list)
            sb.append(e).append(", ");
        //删除最后多余的,
        sb.delete(sb.length() - 2, sb.length());
        sb.append(']');
        return sb.toString();
    }

    public static String toString2(String[] list){
        //StringJoiner sj = new StringJoiner(", ");
        StringJoiner sj = new StringJoiner(", ", "[", "]");
        for(String e : list)
            sj.add(e);
        return sj.toString();
    }
}
